﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

public partial class ManterUsuario : System.Web.UI.Page
{
    public static Boolean inserir;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        inserir = true;
        btCancelar.Enabled = true;
        btGravar.Enabled = true;
        rbTipo.Enabled = true;
        tbNome.Enabled = true;
        tbSenha.Enabled = true;
        tbEmail.Enabled = true;
        tbId.Enabled = true;
        btNovo.Enabled = false;
    }

    private void cancelar()
    {
        tbId.Text = "";
        tbNome.Text = "";
        tbSenha.Text = "";
        tbEmail.Text = "";
        rbTipo.SelectedValue = "1";
        lbErro.Text = "";

        tbId.Enabled = false;
        tbNome.Enabled = false;
        tbSenha.Enabled = false;
        tbEmail.Enabled = false;
        rbTipo.Enabled = false;
        btGravar.Enabled = false;
        btCancelar.Enabled = false;
        btNovo.Enabled = true;


    }

    protected void btCancelar_Click(object sender, EventArgs e)
    {
        cancelar();
    }
    protected void btGravar_Click(object sender, EventArgs e)
    {
        try
        {
            if (inserir)
            {
                Usuario a = new Usuario(tbId.Text, tbNome.Text, tbEmail.Text, Convert.ToInt32(rbTipo.SelectedValue), tbSenha.Text);
                UsuarioBLL ab = new UsuarioBLL();
                ab.Insere(a);
            }
            else
            {
                Usuario s = new Usuario(tbId.Text);
                UsuarioBLL sb = new UsuarioBLL();
                s = sb.Consulta(tbId.Text);
                Usuario a = new Usuario(tbId.Text, tbNome.Text, tbEmail.Text, Convert.ToInt32(rbTipo.SelectedValue), s.Senha);
                UsuarioBLL ab = new UsuarioBLL();
                ab.Atualiza(a);
            }

            cancelar();
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }


    protected void lbRemove_Click(object sender, EventArgs e)
    {
        try
        {
            string id = ((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text;
            Usuario a = new Usuario(id);
            UsuarioBLL ab = new UsuarioBLL();
            ab.Remove(a);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }

    }

    protected void lbAtualiza_Click(object sender, EventArgs e)
    {
        inserir = false;
        try
        {

            string id = ((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text;
            Usuario a = new Usuario(id);
            UsuarioBLL ab = new UsuarioBLL();
            a = ab.Consulta(id);
            tbId.Text = a.Id.ToString();
            tbNome.Text = a.Nome;
            tbEmail.Text = a.Email;
            rbTipo.SelectedValue = a.Tipo.ToString();
            tbSenha.Text = a.Senha;
                

            btCancelar.Enabled = true;
            btGravar.Enabled = true;
            rbTipo.Enabled = true;
            tbNome.Enabled = true;
            tbSenha.Enabled = true;
            tbEmail.Enabled = true;
            btNovo.Enabled = false;
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
}